package discoveries

import (
	"context"
	"discoveries-rpc/proto/articles_srv"
	"discoveries-rpc/server"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

type Discoveries struct {
	articles_srv.UnimplementedArticlesServer
}

func (d *Discoveries) ArticlesCreate(ctx context.Context, req *articles_srv.ArticlesCreateReq) (*articles_srv.ArticlesCreateResp, error) {
	if req.GetIn().Title == "" {
		return nil, status.Errorf(codes.InvalidArgument, "标题不能为空")
	}
	if req.GetIn().Content == "" {
		return nil, status.Errorf(codes.InvalidArgument, "文章内容不能为空")
	}
	if req.GetIn().Author == "" {
		return nil, status.Errorf(codes.InvalidArgument, "作者名称不能为空")
	}
	if req.GetIn().ArticlesImg == "" {
		return nil, status.Errorf(codes.InvalidArgument, "图片不能为空")
	}

	create, err := server.ArticlesCreate(req.GetIn())
	if err != nil {
		return nil, err
	}
	return &articles_srv.ArticlesCreateResp{Info: create}, nil
}

func (d *Discoveries) ArticlesShow(ctx context.Context, req *articles_srv.ArticlesShowReq) (*articles_srv.ArticlesShowResp, error) {
	if req.Id == 0 {
		return nil, status.Errorf(codes.InvalidArgument, "文章不存在")
	}
	show, err := server.ArticlesShow(req.Id)
	if err != nil {
		return nil, err
	}
	return &articles_srv.ArticlesShowResp{Info: show}, nil
}

func (d *Discoveries) ArticlesUpdate(ctx context.Context, req *articles_srv.ArticlesUpdateReq) (*articles_srv.ArticlesUpdateResp, error) {
	if req.GetIn().Id == 0 {
		return nil, status.Errorf(codes.InvalidArgument, "文章不存在")
	}
	update, err := server.ArticlesUpdate(req.GetIn())
	if err != nil {
		return nil, err
	}
	return &articles_srv.ArticlesUpdateResp{Info: update}, nil
}

func (d *Discoveries) ArticlesDelete(ctx context.Context, req *articles_srv.ArticlesDeleteReq) (*articles_srv.ArticlesDeleteResp, error) {
	if req.Id == 0 {
		return nil, status.Errorf(codes.InvalidArgument, "文章不存在")
	}
	articlesDelete, err := server.ArticlesDelete(req.Id)
	if err != nil {
		return nil, err
	}
	return &articles_srv.ArticlesDeleteResp{Info: articlesDelete}, nil
}

func (d *Discoveries) ArticlesList(ctx context.Context, req *articles_srv.ArticlesListReq) (*articles_srv.ArticlesListResp, error) {
	list, err := server.ArticlesList(req.Title)
	if err != nil {
		return nil, err
	}
	return &articles_srv.ArticlesListResp{Info: list}, nil
}

func (d *Discoveries) ReadCountRise(ctx context.Context, req *articles_srv.ReadCountRiseReq) (*articles_srv.ReadCountRiseResp, error) {
	if req.Id == 0 {
		return nil, status.Errorf(codes.InvalidArgument, "文章不存在")
	}
	rise, err := server.ReadCountRise(req.Id)
	if err != nil {
		return nil, err
	}
	return &articles_srv.ReadCountRiseResp{Info: rise}, nil
}

func (d *Discoveries) LikeCountRise(ctx context.Context, req *articles_srv.LikeCountRiseReq) (*articles_srv.LikeCountRiseResp, error) {
	if req.Id == 0 {
		return nil, status.Errorf(codes.InvalidArgument, "文章不存在")
	}
	rise, err := server.LikeCountRise(req.Id)
	if err != nil {
		return nil, err
	}
	return &articles_srv.LikeCountRiseResp{Info: rise}, nil
}

func (d *Discoveries) LikeCountMinimize(ctx context.Context, req *articles_srv.LikeCountMinimizeReq) (*articles_srv.LikeCountMinimizeResp, error) {
	if req.Id == 0 {
		return nil, status.Errorf(codes.InvalidArgument, "文章不存在")
	}
	minimize, err := server.LikeCountMinimize(req.Id)
	if err != nil {
		return nil, err
	}
	return &articles_srv.LikeCountMinimizeResp{Info: minimize}, nil
}
